#include <iostream>
#include <vector>
using namespace std;

class Solution
{
public:
    int maxProfit(vector<int> &prices)
    {
        int lastMax = 0;
        std::vector<int> maxProfits(prices.size(), 0);
        for (int i = prices.size() - 1; i >= 0; i--)
        {
            if (prices[i] > lastMax)
            {
                lastMax = prices[i];
            }
            maxProfits[i] = lastMax;
        }
        int maxProfit = 0;
        for (int i = 0; i < prices.size(); i++)
        {
            int cp = maxProfits[i] - prices[i];
            if (cp > maxProfit)
            {
                maxProfit = cp;
            }
        }
        return maxProfit;
    }
};

int main()
{
    cout << "Code121" << endl;
    Solution s;
    vector<int> prices{7, 2, 4, 1};
    cout << s.maxProfit(prices) << endl;
    return 0;
}